package cn.xunjie.org.controller;

import cn.xunjie.basic.util.AjaxResult;
import cn.xunjie.basic.util.PageList;
import cn.xunjie.org.domain.Employee;
import cn.xunjie.org.query.EmployeeQuery;
import cn.xunjie.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")

public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @GetMapping("/{id}")
    public Employee querById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }
    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }
    //高级查询+分页
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
            try {
                if (employee.getId()==null){
                    employeeService.save(employee);
                }else {
                    employeeService.update(employee);
                }
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me().setSuccess(false).setMessage("瓦了");
            }
    }
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id")Long id){
        try {
            employeeService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("瓦了");
        }
    }
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("瓦了");
        }
    }

}
